package com.xkdx.caipiao;

import java.util.HashMap;
import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.module.network.ErrorModule;
import com.xkdx.caipiao.presistence.login.Login;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.quert.IssueQueryInfo;
import com.xkdx.caipiao.presistence.quert.TypeQueryInfo;
import com.xkdx.caipiao.util.SharePrefenceUtil;

public abstract class ThreadActivity extends FragmentActivity {

	public ProgressDialog dataLoadDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		dataLoadDialog = new ProgressDialog(this);
		dataLoadDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		dataLoadDialog.setCancelable(true);
		dataLoadDialog.setCanceledOnTouchOutside(false);// 解决4.0以上系统触屏后执行onCanel方法
		
	}

	
	public abstract void showOnPost(HashMap<String, AbsModule> result);

	public abstract void startThread();

	public abstract void onBackClick(View view);

	public void toastErrorMessage(HashMap<String, AbsModule> result) {
		Toast.makeText(this, ((ErrorModule) result.get("1")).errorMessage,
				Toast.LENGTH_SHORT).show();
	}

	public void toast(String str) {
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
	}

	public void setLogin(int code) {
		Intent intent = new Intent(ThreadActivity.this, Login.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivityForResult(intent, code);

	}

	public void setLogin() {
		Intent intent = new Intent(ThreadActivity.this, Login.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

		startActivity(intent);

	}

	public void intentMainTabActivity() {
		Intent intent = new Intent(this, MainTabActivity.class);
		startActivity(intent);
	}

	public LoginInfo getLoginInfo() {
		SharePrefenceUtil su = new SharePrefenceUtil(this,
				SharePrefenceUtil.USERINFO);
		return su.getUserInfo();
	}

	public TypeQueryInfo getLotteryType() {
		SharePrefenceUtil su = new SharePrefenceUtil(this,
				SharePrefenceUtil.LOTTERYTYPE);
		return su.getLotteryTypeInfo();
	}

	public IssueQueryInfo getIssueQueryInfo() {
		SharePrefenceUtil su = new SharePrefenceUtil(this,
				SharePrefenceUtil.ISSUEQUERYINFO);
		return su.getIssueQueryInfo();
	}

	public String getLotteryNameFormLotteryID(String str) {
		if (str.equals("dlt")) {
			return getResources().getString(R.string.lotto);
		} else if (str.equals("qxc")) {
			return getResources().getString(R.string.seven_color);
		} else if (str.equals("pls")) {
			return getResources().getString(R.string.arrange_3);
		} else if (str.equals("plw")) {
			return getResources().getString(R.string.arrange_5);
		}
		return null;
	}

	public String getLotteryIDFormLotteryName(String str) {
		if (str.equals(getResources().getString(R.string.lotto))) {
			return "dlt";
		} else if (str.equals(getResources().getString(R.string.seven_color))) {
			return "qxc";
		} else if (str.equals(getResources().getString(R.string.arrange_3))) {
			return "pls";
		} else if (str.equals(getResources().getString(R.string.arrange_5))) {
			return "plw";
		}
		return null;
	}

	public boolean isAppOnForeground() {
		// Returns a list of application processes that are running on the
		// device

		ActivityManager activityManager = (ActivityManager) getApplicationContext()
				.getSystemService(Context.ACTIVITY_SERVICE);
		String packageName = getApplicationContext().getPackageName();

		List<RunningAppProcessInfo> appProcesses = activityManager
				.getRunningAppProcesses();
		if (appProcesses == null)
			return false;

		for (RunningAppProcessInfo appProcess : appProcesses) {
			// The name of the process that this object is associated with.
			if (appProcess.processName.equals(packageName)
					&& appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
				return true;
			}
		}
		return false;
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		if(MyApplication.getInstance().isPaying){
			return;
		}
		if (!isAppOnForeground()) {
			// app 进入后台
			MyApplication.getInstance().startMyNotice();
			// 全局变量isActive = false 记录当前已经进入后台
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		// if (!isActive) {
		// app 从后台唤醒，进入前台

		// isActive = true;
		// }
	}

}
